<?php

namespace App\Models\MainDB\WarOrder;

use Illuminate\Database\Eloquent\Model;

class WarOrderDetail extends Model
{
    protected $table = 'ln_war_order_detail';

    protected $fillable = [
        'id',
        'war_order_id',
        'level',
        'experience',
        'price',
        'add_time',
        'status',//1有效2关闭
    ];

    public $timestamps = false;

    public function getWarOrderDetail($war_order_id)
    {
        $war_order_levels = $this->where(['war_order_id' => $war_order_id, 'status' => 1])->orderBy('level',
            'asc')->orderBy('id', 'asc')->get();
        $war_order_rewards = app('model_war_order_reward')->where(['war_order_id' => $war_order_id])->get();
        $reward_data = [];

        $war_order_user_reward = app('model_war_order_user_reward')->getUserRewardMap(UID, $war_order_id);
        $recommend_list = [];
        foreach ($war_order_rewards as $key => $value) {
            $reward = app('repo_reward')->getRewardDetail($value->reward_id, $value->reward_type, $value->number);
            if (isset($war_order_user_reward[$value->id])) {
                $status = $war_order_user_reward[$value->id];
            } else {
                $status = -1;
            }
            $data = [
                'id'      => $value->id,
                'item_id' => $reward['id'],
                'name'    => $reward['name'],
                'picture' => $reward['picture'],
                'number'  => $reward['number'],
                'status'  => $status,
            ];
            if ($value->type == 1) {
                $reward_data[$value->detail_id]['normal'][] = $data;
            } else {
                $reward_data[$value->detail_id]['special'][] = $data;
            }
            if ($value->recommend == 1) {
                $recommend_list[] = $data;
            }
        }

        $level_list = [];
        foreach ($war_order_levels as $key => $level) {
            $level_list[] = [
                'level'      => $level->level,
                'price'      => $level->price,
                'experience' => $level->experience,
                'normal'     => $reward_data[$level->id]['normal'] ?? [],
                'special'    => $reward_data[$level->id]['special'] ?? [],
            ];
        }

        if (!empty($recommend_list)) {
            $recommend_list = arraySort($recommend_list, 'id', 'desc');
        }

        return ['level_list' => $level_list, 'recommend_list' => array_values($recommend_list)];
    }

    public function getNextLevelExperience($war_order_id, $level)
    {
        $next_level = $this->getNextLevelDetail($war_order_id, $level);
        if (isset($next_level['experience']) && $next_level['experience'] > 0) {
            return $next_level['experience'];
        }

        return 0;
    }

    public function getNextLevel($war_order_id, $level)
    {
        $next_level = $this->getNextLevelDetail($war_order_id, $level);
        if (isset($next_level['level']) && $next_level['level'] > 0) {
            return $next_level['level'];
        }

        return 0;
    }

    public function getNextLevelDetail($war_order_id, $level)
    {
        $list = $this->where(['status' => 1, 'war_order_id' => $war_order_id])->orderBy('level', 'asc')->get();
        if ($list->isEmpty()) {
            return null;
        }
        $max_level = $list->pluck('level')->max();
        $list = $list->toArray();
        if ($level >= $max_level) {
            return end($list);
        }
        foreach ($list as $key => $value) {
            if ($value['level'] > $level) {
                return $value;
            }
        }

        return null;
    }

    public function getLevelExperience($war_order_id, $level)
    {
        $experience = $this->where(['status' => 1, 'level' => $level, 'war_order_id' => $war_order_id])->value('experience');
        return $experience > 0 ? $experience : 0;
    }
}
